//
//  T3FMapView.swift
//  t3_map
//
//  Created by Cunhai Li on 2024/10/22.
//

import UIKit
import CoreLocation
import Flutter

enum T3UserTrackingMode: Int {
    case none = 0
    case follow = 1
    case followWithHeading = 2
}

protocol T3FMapView: UIView {

    var delegate: T3FMapViewDelegate? { get set }

    /// 地图的视图锚点
    var screenAnchor: CGPoint { get set }
    /// 地图中心点
    var centerCoordinate: CLLocationCoordinate2D { get }

    ///是否显示用户位置
    var showsUserLocation: Bool { get set }
    ///定位用户位置的模式
    func setupUserTrackingMode(mode: T3UserTrackingMode, animated: Bool)

    ///当前位置再地图中是否可见
    var userLocationVisible: Bool { get }

    /// 设置地图缩放级别
    /// - Parameters:
    ///   - zoomLevel: CGFloat
    ///   - animated: Bool
    func setupMapView(zoomLevel: CGFloat, animated: Bool)

    /// 设置地图中心点
    /// - Parameters:
    ///   - coordinate: CLLocationCoordinate2D
    ///   - animated: Bool
    func setupCenter(_ coordinate: CLLocationCoordinate2D, animated: Bool)

    /// 地图截图
    func takeSnapshot(completed: @escaping (UIImage?) -> Void)

    /// 将指定view坐标系的坐标转换为经纬度
    func convert(_ point: CGPoint, toCoordinateFrom view: UIView) -> CLLocationCoordinate2D


    // MARK: - func T3FPolyline
    /// 当前地图上的全部线段
    var polylines: [T3FPolyline] { get }
    /// 向地图添加polyline
    func add(polyline options: T3FPolylineOptions) throws -> T3FPolyline
    /// 更新线段路径信息
    func update(polyline options: T3FPolylineOptions)
    /// 路线层级交换
    func exchange(polylines: Array<String>)
    /// 通过polylineOptions.identifier移除已添加的polyline对象，
    func remove(polyline options: T3FPolylineOptions) -> Bool
    /// 在可视区域内显示Polyline
    func show(polylines: [T3FPolylineOptions], edgePadding: UIEdgeInsets?, animated: Bool)

    // MARK: - func T3FAnnotation
    /// 在可视区域内显示标注
    func add(annotations: [T3FAnnotationOptions])
    func remove(annotations: [T3FAnnotationOptions])
    func update(annotation: T3FAnnotationOptions)
    func show(annotations: [T3FAnnotationOptions], edgePadding: UIEdgeInsets?, animated: Bool)

    /// 在可视区域内显示Polyline和Annotation
    func show(polylines: [T3FPolylineOptions], annotations: [T3FAnnotationOptions], edgePadding: UIEdgeInsets?, animated: Bool)


    /// 删除全部覆盖物（包括图层以及图层上的元素）
    func clear()
}

protocol T3FMapViewDelegate: NSObjectProtocol {

    /// 地图加载成功
    func mapViewDidFinishLoadingMap(_ mapView: T3FMapView)

    /// 地图加载完成（在此之后，可以进行坐标计算）
    func mapInitComplete()

    /// 当前位置更新
    func userLocationDidUpdated(location: T3FMapLocation)

    /// 地图区域即将改变时会调用此接口
    func mapView(_ mapView: T3FMapView, regionWillChangeAnimated animated: Bool, wasUserAction: Bool)

    /// 地图区域改变过程中会调用此接口
    func mapViewRegionChanged(_ mapView: T3FMapView, centerCoordinate: CLLocationCoordinate2D, zoomLevel: CGFloat)

    /// 地图区域改变完成后会调用此接口
    func mapView(_ mapView: T3FMapView, regionDidChangeAnimated animated: Bool, wasUserAction: Bool)
    
    /// 路线点击事件
    func mapView(_ mapView: T3FMapView, didTapPolyline routeID: String)

    /// 标注物点击事件
    func mapView(_ mapView: T3FMapView, didAnnotationViewTapped annotationOptions: T3FAnnotationOptions)

}

struct T3FMapManager {

    /// 创建地图入口
    static func createMapView(frame: CGRect, options: T3FMapOptions) -> T3FMapView {

        switch options.mapType {
        case .amap:
            return T3FMAMapView(frame: frame, options: options)
        case .tencentMap:
            return T3FTNMapView(frame: frame, options: options)
        }
    }
}

